<?php

namespace app\common\model;

use app\common\model\BaseModel;


class Cash extends BaseModel
{

    

    

    // 表名
    protected $name = 'cash';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'pay_time_text',
        'createtime_text',
        'status_text'
    ];



    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : 1);
        return is_numeric($value) ? self::fetchStatus($value) : $value;
    }

    public function merchant(){
        return $this->hasOne('merchant', 'id', 'merchant_id');
    }
    public function merchants(){
        return $this->hasOne('merchant', 'id', 'merchant_id')->bind(['merchant_name','merchant_phone']);
    }

    //状态处理
    public static function fetchStatus($status = null){
        $data =  [
            1 => '待审核',
            2 => '已通过',
            3 => '已拒绝',
        ];
        if (isset($data[$status])) {
            return $data[$status];
        }else{
            return $data;
        }
    }


}
